package com.harmony.pilot.knowledge.controller;

import com.harmony.pilot.knowledge.repository.CodeSnippetRepository;
import com.harmony.pilot.knowledge.repository.FileMetadataRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计控制器
 * 提供知识库统计信息
 */
@Slf4j
@RestController
@RequestMapping("/api/knowledge/stats")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class StatsController {
    
    private final FileMetadataRepository fileMetadataRepository;
    private final CodeSnippetRepository snippetRepository;
    
    /**
     * 获取总体统计信息
     * 
     * GET /api/knowledge/stats
     */
    @GetMapping
    public StatsResponse getStats() {
        StatsResponse response = new StatsResponse();
        
        // 文件统计
        response.setTotalFiles(fileMetadataRepository.countTotal());
        response.setScannedFiles(fileMetadataRepository.countScanned());
        
        // 代码片段统计
        response.setTotalSnippets(snippetRepository.count());
        
        // 按类型统计
        Map<String, Long> snippetsByType = new HashMap<>();
        List<Object[]> typeStats = snippetRepository.countBySnippetType();
        for (Object[] stat : typeStats) {
            snippetsByType.put((String) stat[0], (Long) stat[1]);
        }
        response.setSnippetsByType(snippetsByType);
        
        // 按模块统计
        Map<String, Long> filesByModule = new HashMap<>();
        List<Object[]> moduleStats = fileMetadataRepository.countByModuleName();
        for (Object[] stat : moduleStats) {
            if (stat[0] != null) {
                filesByModule.put((String) stat[0], (Long) stat[1]);
            }
        }
        response.setFilesByModule(filesByModule);
        
        // 按文件类型统计
        Map<String, Long> filesByType = new HashMap<>();
        List<Object[]> fileTypeStats = fileMetadataRepository.countByFileType();
        for (Object[] stat : fileTypeStats) {
            filesByType.put((String) stat[0], (Long) stat[1]);
        }
        response.setFilesByType(filesByType);
        
        return response;
    }
    
    /**
     * 获取模块详细统计
     * 
     * GET /api/knowledge/stats/module/{moduleName}
     */
    @GetMapping("/module/{moduleName}")
    public ModuleStatsResponse getModuleStats(@PathVariable String moduleName) {
        ModuleStatsResponse response = new ModuleStatsResponse();
        response.setModuleName(moduleName);
        
        List<com.harmony.pilot.knowledge.entity.FileMetadata> files = 
            fileMetadataRepository.findByModuleName(moduleName);
        
        response.setFileCount(files.size());
        response.setTestFileCount((int) files.stream().filter(f -> f.getIsTest()).count());
        response.setMockFileCount((int) files.stream().filter(f -> f.getIsMock()).count());
        response.setFuzzerFileCount((int) files.stream().filter(f -> f.getIsFuzzer()).count());
        
        return response;
    }
    
    // DTO类
    public static class StatsResponse {
        private long totalFiles;
        private long scannedFiles;
        private long totalSnippets;
        private Map<String, Long> snippetsByType;
        private Map<String, Long> filesByModule;
        private Map<String, Long> filesByType;
        
        // getters and setters
        public long getTotalFiles() { return totalFiles; }
        public void setTotalFiles(long totalFiles) { this.totalFiles = totalFiles; }
        public long getScannedFiles() { return scannedFiles; }
        public void setScannedFiles(long scannedFiles) { this.scannedFiles = scannedFiles; }
        public long getTotalSnippets() { return totalSnippets; }
        public void setTotalSnippets(long totalSnippets) { this.totalSnippets = totalSnippets; }
        public Map<String, Long> getSnippetsByType() { return snippetsByType; }
        public void setSnippetsByType(Map<String, Long> snippetsByType) { this.snippetsByType = snippetsByType; }
        public Map<String, Long> getFilesByModule() { return filesByModule; }
        public void setFilesByModule(Map<String, Long> filesByModule) { this.filesByModule = filesByModule; }
        public Map<String, Long> getFilesByType() { return filesByType; }
        public void setFilesByType(Map<String, Long> filesByType) { this.filesByType = filesByType; }
    }
    
    public static class ModuleStatsResponse {
        private String moduleName;
        private int fileCount;
        private int testFileCount;
        private int mockFileCount;
        private int fuzzerFileCount;
        
        // getters and setters
        public String getModuleName() { return moduleName; }
        public void setModuleName(String moduleName) { this.moduleName = moduleName; }
        public int getFileCount() { return fileCount; }
        public void setFileCount(int fileCount) { this.fileCount = fileCount; }
        public int getTestFileCount() { return testFileCount; }
        public void setTestFileCount(int testFileCount) { this.testFileCount = testFileCount; }
        public int getMockFileCount() { return mockFileCount; }
        public void setMockFileCount(int mockFileCount) { this.mockFileCount = mockFileCount; }
        public int getFuzzerFileCount() { return fuzzerFileCount; }
        public void setFuzzerFileCount(int fuzzerFileCount) { this.fuzzerFileCount = fuzzerFileCount; }
    }
}

